package com.zhz.livegoods.details.service.impl;

import com.zhz.livegoods.details.mapper.ItemMapper;
import com.zhz.livegoods.details.service.DetailsService;
import com.zhz.livegoods.pojo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhz
 * @date 2020/08/05
 **/
@Service
public class DetailsServiceImpl implements DetailsService {
    @Autowired
    private ItemMapper itemMapper;
    @Value("${livegoods.banner.nginx.prefix}")
    private String nginxPrefix;

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    @Override
    @Cacheable(cacheNames = "com:zhz:livegoods:details",key = "'getDetails('+id+')'")
    public Item getDetails(String id) {
        //根据ID查询商品
        Item item=itemMapper.findItemById(id);
        //把图片相对地址改成绝对地址
        List<String> newImgs = new ArrayList<>();
        for(String img : item.getImgs()){
            newImgs.add(nginxPrefix + img);
        }
        item.setImgs(newImgs);
        return item;
    }

}
